package com.zlx.xbhy.mapper;

import com.zlx.xbhy.entity.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface UserMapper extends Mapper<User> {
    @Select("SELECT " +
            "`user`.id, " +
            "`user`.username, " +
            "`user`.`password`, " +
            "`user`.email, " +
            "`user`.qq_openid, " +
            "`user`.wx_openid, " +
            "`user`.real_name, " +
            "`user`.age, " +
            "`user`.phone, " +
            "`user`.gender, " +
            "`user`.info, " +
            "`user`.register_time, " +
            "`user`.login_time, " +
            "`user`.pic, " +
            "`user`.look, " +
            "`user`.is_secret, " +
            "`user`.dept_name, " +
            "`user`.dept_id " +
            "FROM " +
            "`user` " +
            "WHERE " +
            "`user`.username LIKE CONCAT('%',#{username},'%')")
    List<User> selectPage(String username);
    
    
    @Select("SELECT " +
            "u.id, " +
            "u.username, " +
            "u.`password`, " +
            "u.email, " +
            "u.qq_openid, " +
            "u.wx_openid, " +
            "u.real_name, " +
            "u.age, " +
            "u.phone, " +
            "u.gender, " +
            "u.info, " +
            "u.register_time, " +
            "u.login_time, " +
            "u.pic, " +
            "u.look, " +
            "u.is_secret, " +
            "u.dept_name, " +
            "u.dept_id " +
            "FROM " +
            "`user` AS u " +
            "WHERE " +
            "u.id IN (( " +
            "SELECT " +
            "uf.user_focus_id  " +
            "FROM " +
            "userfocus uf  " +
            "WHERE " +
            "uf.user_id = #{userId}  " +
            ")) ")
    List<User> selectFocusUsers(Long userId);

    //查询关注的好友收藏了相同的名单
    @Select("SELECT " +
            "u.*  " +
            "FROM " +
            "`user` u  " +
            "WHERE " +
            "id IN ( SELECT f.u_id FROM favorite f WHERE f.a_id = #{aId} )  " +
            "AND id IN ( " +
            "SELECT " +
            "uf.user_focus_id  " +
            "FROM " +
            "userfocus uf  " +
            "WHERE " +
            "uf.user_id = #{uId})")
    List<User> selectMulFavoriteUser(@Param("uId") Long uId,@Param("aId") Long aId);

    //新增录数
    @Select("SELECT " +
            "COUNT( 1 ) count, " +
            "DATE_FORMAT( DATE_SUB( NOW(), INTERVAL ${day} DAY ), '%m-%d' ) nowDate  " +
            "FROM " +
            "USER  " +
            "WHERE " +
            "to_days( " +
            "now()) - to_days( register_time )= #{day}")
    User countNew(Integer day);
}
